package com.dz.lm.service.usercollection.impl;

import com.dz.lm.common.dto.PageDto;
import com.dz.lm.common.dto.UserCollectionDTO;
import com.dz.lm.common.dto.UserProductDTO;
import com.dz.lm.common.exception.DaoException;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.dao.appLogin.read.AppLoginDao;
import com.dz.lm.dao.usercollection.write.UserCollectionDao;
import com.dz.lm.domain.usercollection.UserCollection;
import com.dz.lm.domain.usercollection.UserCollectionExample;
import com.dz.lm.service.usercollection.UserCollectionService;
import com.dz.lm.service.userproduct.UserProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2020/3/7 21:28;
 */
@Service("userCollectionService")
public class UserCollectionServiceImpl implements UserCollectionService {
    @Resource(name = "writeUserCollectionDao")
    private UserCollectionDao writeUserCollectionDao;
    @Resource(name = "readUserCollectionDao")
    private com.dz.lm.dao.usercollection.read.UserCollectionDao readUserCollectionDao;
    @Resource(name = "readAppLoginDao")
    private AppLoginDao rAppLoginDao;
    @Autowired
    private UserProductService userProductService;

    @Transactional
    @Override
    public int add(int userId, int type, int status, int objId, String remark) throws ServiceException {
        int ret =0;
        UserCollection collection = new UserCollection();
        collection.setUserId(userId);
        collection.setType(type);
        collection.setStatus(status);
        collection.setObjId(objId);
        collection.setRemark(remark);
        try {
             ret = writeUserCollectionDao.insertSelective(collection);
        } catch (DaoException e) {
        }
        return ret;
    }
    @Transactional
    @Override
    public int deltet(int collectId,int userId,int objId,int status,int type) throws ServiceException {
        int ret=0;
        try {
            Map map = new HashMap();
            if(collectId >0){
                map.put("collectId",collectId);
            }
            if(userId >0){
                map.put("userId",userId);
            }
            if(objId >0){
                map.put("objId",objId);
            }
            if(type >0){
                map.put("type",type);
            }
            if(status >0){
                map.put("status",status);
            }
            ret = writeUserCollectionDao.deleteByMap(map);
        } catch (DaoException e) {
        }
        return ret;
    }
    @Transactional
    @Override
    public PageDto query(int pagetNum, int rows, int userId, int objId,int type, int status) throws ServiceException {
        PageDto pageDto = new PageDto();
        PageHelper.startPage(pagetNum,rows);
        UserCollectionExample ex = new UserCollectionExample();
        UserCollectionExample.Criteria cr = ex.createCriteria();
        if(userId>0){
            cr.andUserIdEqualTo(userId);
        }
        if(type>0){
            cr.andTypeEqualTo(type);
        }
        if(status>0){
            cr.andStatusEqualTo(status);
        }
        if(objId > 0){
            cr.andObjIdEqualTo(objId);
        }
        try {
            List<UserCollection> list = readUserCollectionDao.selectByExample(ex);
            if(list!=null && list.size()>0){
                PageInfo pageInfo = new PageInfo(list);
                List<UserCollectionDTO> dtos = new ArrayList();
                for (UserCollection collection : list) {
                    UserCollectionDTO dto = new UserCollectionDTO();
                    BeanUtils.copyProperties(collection,dto);
                    setExt(dto);
                    dtos.add(dto);
                }
                pageDto.setInfo(pageInfo);
                pageDto.setList(dtos);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageDto;

    }
    private void setExt(UserCollectionDTO dto)throws Exception {
        Object o=null;
        switch (dto.getType()){
            case 1://产品
                 o = userProductService.getDetail(0, dto.getObjId(), true);
                if(o!=null){
                    dto.setObject(o);
                }
                break;
            case 2://用户
                Map p1 = new HashMap();
                p1.put("userId", dto.getObjId());
                o = rAppLoginDao.qryUserInfoByUserId(p1);
                if(o!=null){
                    dto.setUserObj(o);
                }
                Map p2 = new HashMap();
                p2.put("userId", dto.getUserId());
                Map user = rAppLoginDao.qryUserInfoByUserId(p2);
                if(user != null ){
                    dto.setUserObject(user);
                }

                break;
        }

    }

}
